#ifndef Material_h__
#define Material_h__



#include "../infLib/infLib.h"
#include "Texture.h"

class Decl;

typedef inf::StaticList<Texture*,8> TextureLayer;

class Material {
    friend class RenderModel;
    friend class MaterialManager;
public:

    Material();
    ~Material();

    const char* GetName() const;

    int         NumLayer() const;
    Texture*    GetTexture( int layerIndex ) const;

    Texture*    GetTextureLayer( textureLayer_t layer );
    void        BindTextureLayer( textureLayer_t layer, Texture* texture );

    void        Apply();

    inf::Str    name;

    inf::Color4     ambientColor;
    inf::Color4     diffuseColor;
    inf::Color4     specularColor;
    float           shinness;

    // 0 -- diffuse
    // 1 -- specular
    // ...
    TextureLayer    textureLayer;
protected:
    void            InitFromDecl( const Decl* decl );
};


typedef Material*   MaterialPtr;
typedef inf::HashMap<MaterialPtr>   MaterialMap;

class MaterialManager {
public:
    MaterialManager();
    ~MaterialManager();

    void        Init();
    void        Shutdown();

    void        ImportMaterialFromDeclManager();
    int         NumMaterials() const;

    Material*   FindMaterial( const char* mtlName, bool makeDefault = true );
    Material*   MaterialByIndex( int i ) const;

    Material*   GetDefaultMaterial();
    Material*   GetErrorMaterial();
private:
    void        AddMaterial( Material* mtl );

    MaterialMap materials;

    Material*   defaultMaterial;
    Material*   errorMaterial;
};

extern MaterialManager* materialManager;

#endif // Material_h__